package com.academy.student;

import java.util.ArrayList;
import java.util.List;

import com.academy.querystapp.R;
import com.academy.teacher.QuestionDetail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AnswerAdapter extends ArrayAdapter<QuestionDetail>
{
	private LayoutInflater inflater;
	List<QuestionDetail> questionDetail;
	private int resourceID;
	private AnswerListActivity answerListActivity;
	
	public AnswerAdapter(Context context, int resourceID,List<QuestionDetail> questiondetail,AnswerListActivity answerListActivity) 
	{
		super(context, resourceID, questiondetail);
		
		this.resourceID=resourceID;
		this.questionDetail= (ArrayList<QuestionDetail>) questiondetail;
		this.answerListActivity=answerListActivity;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public int getCount() 
	{
		return questionDetail.size();
	}

	public long getItemId(int position)
	{
		return position;
	}
	
	static class ViewHolder
	{
		public TextView txtQuestion;
		public Button btnShowQuery;
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder viewHolder;
		
		if(convertView == null)
		{
			convertView = inflater.inflate(resourceID, null);
			viewHolder = new ViewHolder();
			viewHolder.txtQuestion = (TextView)convertView.findViewById(R.id.txtQuestion);
			viewHolder.btnShowQuery = (Button)convertView.findViewById(R.id.btnShowQuery);
			
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder)convertView.getTag();			
		}
		
		viewHolder.txtQuestion.setTag(position);
		viewHolder.btnShowQuery.setTag(position);
		viewHolder.btnShowQuery.setOnClickListener(new OnClickListener()
		{			
			@Override
			public void onClick(View v)
			{
				answerListActivity.passIntentToAnswerActivity((Integer) v.getTag());
				
			}
		});
		viewHolder.txtQuestion.setText(questionDetail.get(position).getQuestion());
	
		return convertView;
	}
}
